﻿Option Strict On
Option Explicit On 
Imports System.IO
Imports IP.Core.IPCommon
Imports IP.Core.IPException
Imports IP.Core.IPUserService
Imports IP.Core.IPData
Imports IP.Core.IPBusinessService
Imports IP.Core.IPDataControl

Class f203_thay_doi_mat_khau
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        format_control()
    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents ImageList As System.Windows.Forms.ImageList
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents m_btnOK As SIS.Controls.Button.SiSButton
    Friend WithEvents m_btnCancel As SIS.Controls.Button.SiSButton
    Friend WithEvents m_txt_mat_khau_cu As System.Windows.Forms.TextBox
    Friend WithEvents m_txt_ten_dang_nhap As System.Windows.Forms.TextBox
    Friend WithEvents m_txt_go_lai As System.Windows.Forms.TextBox
    Friend WithEvents m_txt_mat_khau_moi As System.Windows.Forms.TextBox
    Friend WithEvents Label9 As System.Windows.Forms.Label
    Friend WithEvents Label11 As System.Windows.Forms.Label
    Friend WithEvents Label10 As System.Windows.Forms.Label
    Friend WithEvents Label6 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(f203_thay_doi_mat_khau))
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.Label9 = New System.Windows.Forms.Label
        Me.m_txt_mat_khau_cu = New System.Windows.Forms.TextBox
        Me.m_txt_ten_dang_nhap = New System.Windows.Forms.TextBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label6 = New System.Windows.Forms.Label
        Me.GroupBox2 = New System.Windows.Forms.GroupBox
        Me.Label11 = New System.Windows.Forms.Label
        Me.Label10 = New System.Windows.Forms.Label
        Me.m_txt_go_lai = New System.Windows.Forms.TextBox
        Me.m_txt_mat_khau_moi = New System.Windows.Forms.TextBox
        Me.Label3 = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        Me.ImageList = New System.Windows.Forms.ImageList(Me.components)
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.m_btnOK = New SIS.Controls.Button.SiSButton
        Me.m_btnCancel = New SIS.Controls.Button.SiSButton
        Me.GroupBox1.SuspendLayout()
        Me.GroupBox2.SuspendLayout()
        Me.Panel1.SuspendLayout()
        Me.SuspendLayout()
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.Label9)
        Me.GroupBox1.Controls.Add(Me.m_txt_mat_khau_cu)
        Me.GroupBox1.Controls.Add(Me.m_txt_ten_dang_nhap)
        Me.GroupBox1.Controls.Add(Me.Label2)
        Me.GroupBox1.Controls.Add(Me.Label1)
        Me.GroupBox1.Controls.Add(Me.Label6)
        Me.GroupBox1.Dock = System.Windows.Forms.DockStyle.Top
        Me.GroupBox1.Location = New System.Drawing.Point(0, 0)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(280, 64)
        Me.GroupBox1.TabIndex = 0
        Me.GroupBox1.TabStop = False
        '
        'Label9
        '
        Me.Label9.AutoSize = True
        Me.Label9.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label9.ForeColor = System.Drawing.Color.Red
        Me.Label9.Location = New System.Drawing.Point(243, 19)
        Me.Label9.Name = "Label9"
        Me.Label9.Size = New System.Drawing.Size(12, 13)
        Me.Label9.TabIndex = 6
        Me.Label9.Text = "*"
        '
        'm_txt_mat_khau_cu
        '
        Me.m_txt_mat_khau_cu.Location = New System.Drawing.Point(112, 38)
        Me.m_txt_mat_khau_cu.Name = "m_txt_mat_khau_cu"
        Me.m_txt_mat_khau_cu.PasswordChar = Global.Microsoft.VisualBasic.ChrW(42)
        Me.m_txt_mat_khau_cu.Size = New System.Drawing.Size(128, 20)
        Me.m_txt_mat_khau_cu.TabIndex = 5
        '
        'm_txt_ten_dang_nhap
        '
        Me.m_txt_ten_dang_nhap.BackColor = System.Drawing.SystemColors.HighlightText
        Me.m_txt_ten_dang_nhap.Enabled = False
        Me.m_txt_ten_dang_nhap.Location = New System.Drawing.Point(112, 14)
        Me.m_txt_ten_dang_nhap.Name = "m_txt_ten_dang_nhap"
        Me.m_txt_ten_dang_nhap.Size = New System.Drawing.Size(128, 20)
        Me.m_txt_ten_dang_nhap.TabIndex = 2
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Location = New System.Drawing.Point(39, 41)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(70, 13)
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "Mật khẩu cũ "
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(28, 17)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(81, 13)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Tên đăng nhập"
        '
        'Label6
        '
        Me.Label6.AutoSize = True
        Me.Label6.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label6.ForeColor = System.Drawing.Color.Red
        Me.Label6.Location = New System.Drawing.Point(243, 43)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(12, 13)
        Me.Label6.TabIndex = 4
        Me.Label6.Text = "*"
        '
        'GroupBox2
        '
        Me.GroupBox2.Controls.Add(Me.Label11)
        Me.GroupBox2.Controls.Add(Me.Label10)
        Me.GroupBox2.Controls.Add(Me.m_txt_go_lai)
        Me.GroupBox2.Controls.Add(Me.m_txt_mat_khau_moi)
        Me.GroupBox2.Controls.Add(Me.Label3)
        Me.GroupBox2.Controls.Add(Me.Label4)
        Me.GroupBox2.Dock = System.Windows.Forms.DockStyle.Fill
        Me.GroupBox2.Location = New System.Drawing.Point(0, 64)
        Me.GroupBox2.Name = "GroupBox2"
        Me.GroupBox2.Size = New System.Drawing.Size(280, 77)
        Me.GroupBox2.TabIndex = 1
        Me.GroupBox2.TabStop = False
        '
        'Label11
        '
        Me.Label11.AutoSize = True
        Me.Label11.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label11.ForeColor = System.Drawing.Color.Red
        Me.Label11.Location = New System.Drawing.Point(243, 21)
        Me.Label11.Name = "Label11"
        Me.Label11.Size = New System.Drawing.Size(12, 13)
        Me.Label11.TabIndex = 7
        Me.Label11.Text = "*"
        '
        'Label10
        '
        Me.Label10.AutoSize = True
        Me.Label10.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label10.ForeColor = System.Drawing.Color.Red
        Me.Label10.Location = New System.Drawing.Point(244, 45)
        Me.Label10.Name = "Label10"
        Me.Label10.Size = New System.Drawing.Size(12, 13)
        Me.Label10.TabIndex = 6
        Me.Label10.Text = "*"
        '
        'm_txt_go_lai
        '
        Me.m_txt_go_lai.Location = New System.Drawing.Point(112, 40)
        Me.m_txt_go_lai.Name = "m_txt_go_lai"
        Me.m_txt_go_lai.PasswordChar = Global.Microsoft.VisualBasic.ChrW(42)
        Me.m_txt_go_lai.Size = New System.Drawing.Size(128, 20)
        Me.m_txt_go_lai.TabIndex = 5
        '
        'm_txt_mat_khau_moi
        '
        Me.m_txt_mat_khau_moi.Location = New System.Drawing.Point(112, 16)
        Me.m_txt_mat_khau_moi.Name = "m_txt_mat_khau_moi"
        Me.m_txt_mat_khau_moi.PasswordChar = Global.Microsoft.VisualBasic.ChrW(42)
        Me.m_txt_mat_khau_moi.Size = New System.Drawing.Size(128, 20)
        Me.m_txt_mat_khau_moi.TabIndex = 2
        '
        'Label3
        '
        Me.Label3.AutoSize = True
        Me.Label3.Location = New System.Drawing.Point(38, 19)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(71, 13)
        Me.Label3.TabIndex = 0
        Me.Label3.Text = "Mật khẩu mới"
        '
        'Label4
        '
        Me.Label4.AutoSize = True
        Me.Label4.Location = New System.Drawing.Point(75, 43)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(34, 13)
        Me.Label4.TabIndex = 3
        Me.Label4.Text = "Gõ lại"
        '
        'ImageList
        '
        Me.ImageList.ImageStream = CType(resources.GetObject("ImageList.ImageStream"), System.Windows.Forms.ImageListStreamer)
        Me.ImageList.TransparentColor = System.Drawing.Color.Transparent
        Me.ImageList.Images.SetKeyName(0, "")
        Me.ImageList.Images.SetKeyName(1, "")
        '
        'Panel1
        '
        Me.Panel1.Controls.Add(Me.m_btnOK)
        Me.Panel1.Controls.Add(Me.m_btnCancel)
        Me.Panel1.Dock = System.Windows.Forms.DockStyle.Bottom
        Me.Panel1.Location = New System.Drawing.Point(0, 141)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(280, 32)
        Me.Panel1.TabIndex = 2
        '
        'm_btnOK
        '
        Me.m_btnOK.AdjustImageLocation = New System.Drawing.Point(0, 0)
        Me.m_btnOK.BtnShape = SIS.Controls.Button.emunType.BtnShape.Rectangle
        Me.m_btnOK.BtnStyle = SIS.Controls.Button.emunType.XPStyle.[Default]
        Me.m_btnOK.Dock = System.Windows.Forms.DockStyle.Right
        Me.m_btnOK.Font = New System.Drawing.Font("Times New Roman", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.m_btnOK.ForeColor = System.Drawing.SystemColors.ActiveCaption
        Me.m_btnOK.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.m_btnOK.ImageIndex = 1
        Me.m_btnOK.ImageList = Me.ImageList
        Me.m_btnOK.Location = New System.Drawing.Point(109, 0)
        Me.m_btnOK.Name = "m_btnOK"
        Me.m_btnOK.Size = New System.Drawing.Size(80, 32)
        Me.m_btnOK.TabIndex = 0
        Me.m_btnOK.Text = "Đồng ý"
        '
        'm_btnCancel
        '
        Me.m_btnCancel.AdjustImageLocation = New System.Drawing.Point(0, 0)
        Me.m_btnCancel.BtnShape = SIS.Controls.Button.emunType.BtnShape.Rectangle
        Me.m_btnCancel.BtnStyle = SIS.Controls.Button.emunType.XPStyle.[Default]
        Me.m_btnCancel.Dock = System.Windows.Forms.DockStyle.Right
        Me.m_btnCancel.Font = New System.Drawing.Font("Times New Roman", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.m_btnCancel.ForeColor = System.Drawing.SystemColors.ActiveCaption
        Me.m_btnCancel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.m_btnCancel.ImageIndex = 0
        Me.m_btnCancel.ImageList = Me.ImageList
        Me.m_btnCancel.Location = New System.Drawing.Point(189, 0)
        Me.m_btnCancel.Name = "m_btnCancel"
        Me.m_btnCancel.Size = New System.Drawing.Size(91, 32)
        Me.m_btnCancel.TabIndex = 1
        Me.m_btnCancel.Text = "Thoát (Esc)"
        '
        'f203_thay_doi_mat_khau
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(280, 173)
        Me.Controls.Add(Me.GroupBox2)
        Me.Controls.Add(Me.Panel1)
        Me.Controls.Add(Me.GroupBox1)
        Me.MaximizeBox = False
        Me.Name = "f203_thay_doi_mat_khau"
        Me.Text = "F203 - Thay doi mat khau"
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox1.PerformLayout()
        Me.GroupBox2.ResumeLayout(False)
        Me.GroupBox2.PerformLayout()
        Me.Panel1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region
#Region "PUBLIC INTERFACES"
    Public Sub displayLogin()
        '*********************************************************************
        '*dung de thay doi mat khau
        '*********************************************************************
        'Hiện thị cửa sổ
        Me.ShowDialog()
    End Sub
#End Region
#Region "Data Structure"
    Private C_MSG_UserNamePWD_Not_Correct As String = ""
#End Region

#Region "private Methods"
    Private Sub format_control()
        CControlFormat.setFormStyle(Me, IPFormStyle.DialogForm)
    End Sub
    Private Sub set_initial_form_load()

        m_txt_ten_dang_nhap.Text = CApplicationContext.get_curent_user()
    End Sub
    Private Function CheckLoginStatus(ByVal i_str_username As String, _
                                      ByVal i_str_pwd As String) As Boolean
        Try
            Dim v_str_login_status As String = New US_HT_PHAN_QUYEN_HE_THONG().check_login_status( _
                                    i_str_username, _
                                    i_str_pwd)
            If (v_str_login_status = US_HT_PHAN_QUYEN_HE_THONG.C_LOGIN_USER_NORMAL) Then
                Return True
            End If

            Return False
        Catch
            Return False
        End Try
    End Function
    Private Function CheckPasswordCanChange(ByVal i_str_user_name As String) As Boolean
        Try
            Return New US_HT_NGUOI_SU_DUNG().check_passwor_can_change(i_str_user_name)
        Catch
            Return False
        End Try
        Return False
    End Function
    Private Function check_validate() As Boolean
        Dim v_i, v_j As Integer

        v_i = 0
        If (m_txt_go_lai.Text = "") Then
            BaseMessages.MsgBox_Error("Dữ liệu cần đuợc cập nhật")
            m_txt_go_lai.Focus()
            Return False
        End If
        If (m_txt_mat_khau_cu.Text = "") Then
            BaseMessages.MsgBox_Error("Dữ liệu cần đuợc cập nhật")
            m_txt_mat_khau_cu.Focus()
            Return False
        End If
        If (m_txt_mat_khau_moi.Text = "") Then
            BaseMessages.MsgBox_Error("Dữ liệu cần đuợc cập nhật")
            m_txt_mat_khau_moi.Focus()
            Return False
        End If
        If m_txt_mat_khau_moi.Text <> m_txt_go_lai.Text Then
            BaseMessages.MsgBox_Error("Mật khẩu gõ lại không khớp !")
            m_txt_mat_khau_moi.Focus()
            Return False
        End If
        ' Quang sua 22/10/2007 ko dc co dau cach trong mat khau
        While (v_i < Len(m_txt_mat_khau_moi.Text) - 1)
            If m_txt_mat_khau_moi.Text.Chars(v_i) = " " And m_txt_mat_khau_moi.Text.Chars(v_i + 1) <> " " Then
                BaseMessages.MsgBox_Error("Mật khẩu không thể có dấu cách !")
                m_txt_mat_khau_moi.Focus()
                Return False
            End If
            v_i += 1
        End While
        ' Quang sua 27/11/2007 ko dc nhap toan` so vao trong mat khau
        If CUtil.IsValidNumber(m_txt_mat_khau_moi.Text, True) Then
            BaseMessages.MsgBox_Error("Mật khẩu không thể toàn số !")
            m_txt_mat_khau_moi.Focus()
            Return False
        End If
        Return True
    End Function
    Private Sub save_data()
        If check_validate() = True Then
            Dim v_us As New US_HT_NGUOI_SU_DUNG
            Dim v_ds As New DS_HT_NGUOI_SU_DUNG
            Dim v_str_old_username, v_str_old_password As String
            v_str_old_username = CProvider.getUserName()
            v_str_old_password = CProvider.getPassWord()

            CProvider.SetConnectionParameter( _
                    CProvider.getDataSource(), _
                    v_str_old_username, _
                    m_txt_mat_khau_cu.Text)

            If CheckPasswordCanChange(m_txt_ten_dang_nhap.Text) = True Then
                BaseMessages.MsgBox_Infor("Bạn không được thay đổi mật khẩu")
            ElseIf CheckLoginStatus(m_txt_ten_dang_nhap.Text, m_txt_mat_khau_cu.Text) = False Then
                BaseMessages.MsgBox_Error("Mật khẩu cũ không đúng !")
            Else
                v_us.FillDataSet_By_username(m_txt_ten_dang_nhap.Text)
                v_us.strMAT_KHAU = m_txt_mat_khau_moi.Text
                v_us.open_connection()
                v_us.Update()
                v_us.commit_close_connection()
                BaseMessages.MsgBox_Infor("Mật khẩu đã được thay đổi, bạn hãy khởi động lại chuơng trình !")
            End If
            CProvider.SetConnectionParameter( _
                    CProvider.getDataSource(), _
                    v_str_old_username, _
                    v_str_old_password)
        End If
    End Sub
#End Region

    Private Sub f203_thay_doi_mat_khau_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        set_initial_form_load()
    End Sub

    Private Sub m_btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles m_btnCancel.Click
        Try
            Me.Close()
        Catch ex As Exception
            CSystemLog_301.ExceptionHandle(ex)
        End Try
    End Sub

    Private Sub m_btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles m_btnOK.Click
        Try
            save_data()
        Catch ex As Exception
            CSystemLog_301.ExceptionHandle(ex)
        End Try
    End Sub

    Private Sub f203_thay_doi_mat_khau_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Try
            Select Case e.KeyCode
                Case Keys.Escape
                    Me.Close()
                Case Keys.Enter
                    save_data()
            End Select
        Catch ex As Exception
            CSystemLog_301.ExceptionHandle(ex)
        End Try
    End Sub
End Class
